//
//  ActionsListViewController.m
//  PacIsBack
//
//  Created by Simon André on 13/02/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ActionsListViewController.h"
#import "CharacterProtocol.h";
#import "BattleProtocol.h"
#import "Constants.h"
#import "AbilityProtocol.h"
#import "ActionElementViewController.h"


@implementation ActionsListViewController
@synthesize mainView, currentElement, battle, character, elementList;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

-(id) initForCharacter:(id<CharacterProtocol>) shownCharacter{
	if (self = [super init]) {
		[self setCharacter:shownCharacter];
		[self setBattle:[shownCharacter currentBattle]];
		UIScrollView* emptyView = [[UIScrollView alloc] initWithFrame:CGRectMake(actionListPositionX, actionListPositionY, actionListWidth, actionListHeight)];
		[self setMainView:emptyView];
		[self setView:[self mainView]];
		NSArray* availableActions = [shownCharacter availableSkills];
		NSMutableArray* list = [[NSMutableArray alloc] init];
		CGFloat actionsSize = 0;
		for (id<AbilityProtocol> ability in availableActions) {
			ActionElementViewController* elem = [[ActionElementViewController alloc] initWithAction:ability andList:self];
			if (actionsSize<=0.0) {
				[elem select];
			}
			[[self view] addSubview:[elem view]];
			[[elem view] setCenter:CGPointMake((actionListPositionX+actionListWidth)/2, actionsSize+([[elem view] frame].size.height)/2)];
			actionsSize = actionsSize+([[elem view] frame].size.height);
			[list addObject:elem];
			[elem release];
		}
		[self setElementList:list];
		[list release];
		[emptyView setContentSize:CGSizeMake(actionListWidth, actionsSize)];
		[emptyView release];
	}
	return self;
}

-(void) selectAbility:(ActionElementViewController*) selectedAbility{
	if ([character currentMP]>=[[selectedAbility ability] mpCost]) {
		if ([self currentElement]!=nil && [self currentElement]!=selectedAbility) {
			[[self currentElement] unselect];
		}
		[self setCurrentElement:selectedAbility];
		[[self battle] setSelectedAbility:[selectedAbility ability]];
	}
	else {
		[selectedAbility unselect];
	}

}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[self setMainView:nil];
	[self setCharacter:nil];
	[self setCurrentElement:nil];
	[self setBattle:nil];
	[self setElementList:nil];
	[super dealloc];
}


@end
